//
//  CYGeRenZhuYe_ShiPinViewController.m
//  cyydt
//
//  Created by chu on 2019/1/27.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYGeRenZhuYe_ShiPinViewController.h"

#import "CYHomeVideoDetailsViewController.h"
#import "CYGeRenZhuYeTableViewCell.h"

@interface CYGeRenZhuYe_ShiPinViewController ()<UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
{
    NSString *_itemId;
    NSString *_userId;
}
@property (nonatomic, strong) NSMutableArray *dataSourceArr;

@property (nonatomic, strong) NSMutableArray *noFollowDataSourceArr;

@property (nonatomic, strong) NSMutableArray *dynamicsArr;

@end

@implementation CYGeRenZhuYe_ShiPinViewController
- (instancetype)initWithItemId:(NSString *)itemId UserId:(NSString *)userid{
    if (self = [super init]) {
        _itemId = itemId;
        _userId = userid;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableView];
    [self getData];
    self.tableView.estimatedRowHeight = 0;

    if (@available(iOS 11.0, *)) {
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CYGeRenZhuYeTableViewCell *cell = [CYGeRenZhuYeTableViewCell initWithTableView:tableView Identifier:@"CYGeRenZhuYeTableViewCell"];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"CYGeRenZhuYeTableViewCell" owner:nil options:nil][0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataSourceArr[indexPath.row];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CYGeRenZhuYeTableViewCell *cell = [CYGeRenZhuYeTableViewCell initWithTableView:tableView Identifier:@"CYGeRenZhuYeTableViewCell"];
    
    cell.model = self.dataSourceArr[indexPath.row];
    
    return cell.cellHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    return view;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CYProjectModel *model = self.dataSourceArr[indexPath.row];
    
    CYHomeVideoDetailsViewController *detail = [[CYHomeVideoDetailsViewController alloc] init];
    detail.videoId = [model.ID stringValue];
    [[AppDelegate sharedAppDelegate] pushViewController:detail];
    
}
#pragma mark - DZNEmptyDataSetDelegate
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"wuneir"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"当前页面没有内容！";
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:RGBOF(0x999999)
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    // 空白页面被点击时开启动画，reloadEmptyDataSet
    [self getData];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView{
    return 120;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = BackGroundColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getData];
        }];
        
        _tableView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
            [self getMoreData];
        }];
        
        [_tableView registerNib:[UINib nibWithNibName:@"CYGeRenZhuYeTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYGeRenZhuYeTableViewCell"];
        
    }
    return _tableView;
}

#pragma mark - 动态
- (void)getData{
    self.page = 1;
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"dynamic"];
    NSDictionary *param = @{@"page":@1, @"userid":_userId, @"type":@"28", @"itemid":_itemId};
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"动态 ---- %@",responseResult);
        self.tableView.emptyDataSetSource = self;
        self.tableView.emptyDataSetDelegate = self;
        if ([self.tableView.mj_header isRefreshing]) {
            [self.tableView.mj_header endRefreshing];
        }
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                NSDictionary *data = responseResult[@"data"];
                NSArray *list = data[@"list"];
                [self.dataSourceArr removeAllObjects];
                for (NSDictionary *dic in list) {
                    CYProjectModel *model = [CYProjectModel mj_objectWithKeyValues:dic];
                    [self.dataSourceArr addObject:model];
                }
            }else{
                
            }
            [self.tableView reloadData];
            
        }
    }];
}

- (void)getMoreData{
    self.page ++;
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"dynamic"];
    NSDictionary *param = @{@"page":[NSNumber numberWithInteger:self.page], @"userid":_userId, @"type":@"28", @"itemid":_itemId};
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"动态 ---- %@",responseResult);
        if ([self.tableView.mj_footer isRefreshing]) {
            [self.tableView.mj_footer endRefreshing];
        }
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
            self.page--;
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                NSDictionary *data = responseResult[@"data"];
                NSArray *list = data[@"list"];
                for (NSDictionary *dic in list) {
                    CYProjectModel *model = [CYProjectModel mj_objectWithKeyValues:dic];
                    [self.dataSourceArr addObject:model];
                }
            }else{
                self.page --;
            }
            [self.tableView reloadData];
        }
    }];
}

- (NSMutableArray *)dataSourceArr{
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArr;
}

@end
